<!DOCTYPE html>
<html>
<head>
    <include file="Public:header" />
    <!-- jqgrid-->
    <link href="__ACSS__/plugins/jqgrid/ui.jqgrid.css?0820" rel="stylesheet">
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox ">
                    <div class="ibox-content">
                    	<div class="ibox-search-box">
                    		<div class="row">
	                    		<div class="infont col-md-2">
		                    		<input type="text" name="name" class="form-control" placeholder="规格名">
								</div>
	                    		<div class="infont col-md-2">
	                    			<button class="btn btn-success btn-block btn-ibox-search" type="button">搜索</button>
	                    		</div>
                    		</div>
                    	</div>
                    	<p>
                    		<button type="button" id="add" class="btn btn-white btn-sm btn-outline"><i class="fa fa-plus-square-o"></i>&nbsp;新增</button>
                    		<button type="button" id="edit" class="btn btn-white btn-sm btn-outline"><i class="fa fa-edit"></i>&nbsp;修改</button>
                    		<button type="button" id="del" class="btn btn-white btn-sm btn-outline"><i class="fa fa-trash-o"></i>&nbsp;删除</button>
                    	</p>
                        <div class="jqGrid_wrapper">
                            <table id="table_list"></table>
                            <div id="pager_list"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<!-- 全局js -->
<include file="Public:foot" />
<!-- jqGrid -->
<script src="__AJSPLUGINS__/jqgrid/i18n/grid.locale-cn.js?0820"></script>
<script src="__AJSPLUGINS__/jqgrid/jquery.jqGrid.min.js?0820"></script>
<!-- Page-Level Scripts -->
<script>
$(document).ready(function () {
	//初始化
    $.jgrid.defaults.styleUI = 'Bootstrap';
    $("#table_list").jqGrid({
    	url : '__URL__/getAllData',
    	datatype: "json",
        height: 580,
        autowidth: true,
        shrinkToFit: true,
        rowNum: 15,
        rowList: [10, 15, 30, 50],
        colNames: ['ID', '规格名'],
        colModel: [ {name : 'id',index : 'id'},
                    {name : 'name',index : 'name'},
                  ],
        pager: "#pager_list",
        viewrecords: true,
        hidegrid: false,
        sortname: "id"
    });
    
    //搜索
    $(".btn-ibox-search").click(function() {
    	result = getSearchParamByInput();
    	$("#table_list").jqGrid('setGridParam', { 
    		page: 1,
    		url : '__URL__/getAllData' + result
    	}).trigger("reloadGrid");
    })
    //监控回车
    $(".ibox-search-box").find(":input").keydown(function(event) {
		if(event.keyCode == 13){
			$(".btn-ibox-search").trigger("click");
		}
	})
    
    //获取搜索条件（仅限封装简单条件）
    function getSearchParamByInput() {
    	var result = "";
    	$(".ibox-search-box").find(":input").each(function() {
    		if($(this).val() != "") {
    			if(result != "") {
    				result += "&";
    			}else{
    				result += "?";
    			}
    			result += $(this).attr("name") + "=" + $(this).val();
    		}
    	})
    	return result;
    }
	
 	// 设置按钮
    $("#table_list").jqGrid('navGrid', '#pager_list', {
        edit: false,
        add: false,
        del: false,
        search: false
    }, {
        height: 200,
        reloadAfterSubmit: true
    });

  	//增
	$("#add").click(function() {
		layerDialog("添加规格",'__URL__/add',"90%","50%",true);
	})
	
    //改
    $("#edit").click(function() {
    	var id = $("#table_list").jqGrid('getGridParam','selrow');
    	if(id != null) {
    		layerDialog("修改",'__URL__/edit?id='+id,"90%","50%",true);
    	}else {
    		layer.msg("您没有选取数据！");
    	}
    })
    

     //删
     $("#del").click(function() {
     	var id = $("#table_list").jqGrid('getGridParam','selrow');
     	if(id != null) {
     		layer.confirm('确定执行？', function(index){
         		$.ajax({
         			url:"__URL__/logicDel",
         			type:"post",
         			data:{'id':id},
         			success:function(json){
         				var data = eval("("+json+")");
         				if(data.status == 1){
         					layer.msg(data.info);
         					$('#table_list').trigger("reloadGrid");
         				}else{
         					layer.msg(data.info);
         				}
         			}
         		})
         	    layer.close(index);
         	});
     	}else {
     		layer.msg("您没有选取数据！");
     	}
     })
});
//更改状态
function updateStatus(id,is_on){
	layer.confirm('确定执行？', function(index){
		$.ajax({
			url:"__URL__/updateStatus",
			type:"post",
			data:{'id':id,'field':'is_on','islock':is_on},
			success:function(json){
				var data = eval("("+json+")");
				if(data.status == 1){
					layer.msg(data.info, {time: 1000});
				}else{
					layer.msg(data.info, {time: 1000});
					$('#table_list').trigger("reloadGrid");
				}
				$('#table_list').trigger("reloadGrid");
			}
		})
		layer.close(index);
	})
}
$("#type_id").change(function() {
	var index = layer.load();
	$.ajax({
		url:"__URL__/getSubtypes",
		type:"post",
		data:{'id':$(this).find("option:selected").val()},
		success:function(json){
			var data = eval("("+json+")").data;
			var html = "";
			if(data != null){
				html += '<option value="0">请选择</option>';
				for(var j=0; j<data.length; j++){
					html += '<option value="'+ data[j].subtype_code +'">'+ data[j].subtype_name +'</option>'
				}
			}
			$("#subtype_code").html(html);
			layer.close(index);
		}
	})
})
</script>
</html>